package com.hengda.museumonline.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.BaseLoadMoreHeaderAdapter;
import com.hengda.museumonline.adapter.DomesticAdapter;
import com.hengda.museumonline.adapter.MainBannerAdapter;
import com.hengda.museumonline.entity.DomesticBean;
import com.hengda.museumonline.entity.InternationalBean;
import com.hengda.museumonline.entity.NoticeBean;
import com.hengda.museumonline.fragment.DomesticFragment;
import com.hengda.museumonline.fragment.InternationalFragment;
import com.hengda.museumonline.fragment.NoticeFragment;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.http.GetData;
import com.hengda.museumonline.listener.AppBarStateChangeListener;
import com.hengda.museumonline.ui.LoginActivity;
import com.hengda.museumonline.ui.MainActivity;
import com.hengda.museumonline.ui.SearchingActivity;
import com.hengda.museumonline.ui.SettingActivity;
import com.hengda.museumonline.utils.AutoPlayUtil;
import com.hengda.museumonline.utils.RefreshUtil;
import com.hengda.museumonline.utils.TabIconsUtil;
import com.hengda.museumonline.view.AutoPlayViewPager;
import com.hengda.museumonline.view.MySwipeRefreshLayout;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/6.
 */

public class InfoPager implements View.OnClickListener {
    public View view;
    private AutoPlayViewPager autoPlayViewPager;
    private ViewPager viewpager;
    private TabLayout tabLayout;
    private static final int DomesticTag = 1;
    private static final int InternationalTag = 2;
    private CollapsingToolbarLayout collapsingtoolbarlayout;
    private AppBarLayout appbar;
    private MainActivity mainActivity;
    private Toolbar toolbar;
    private TextView tvtitle;
    private ImageView setting;
    private ImageView searching;
    private MySwipeRefreshLayout refreshLayout;
    private LinearLayout ll;
    private String[] titles = {"国内", "国际", "通知"};
    private int[] tabIcons = {
            R.drawable.info_domestic2,
            R.drawable.info_international2,
            R.drawable.info_notice2};
    private int[] tabIconsPressed = {
            R.drawable.info_domestic1,
            R.drawable.info_international1,
            R.drawable.info_notice1};

    public InfoPager(MainActivity activity) {
        mainActivity = activity;
        initView();
        initData();
    }


    public void initView() {
        view = View.inflate(mainActivity, R.layout.infopager, null);
        refreshLayout = (MySwipeRefreshLayout) view.findViewById(R.id.refresh);
        ll = (LinearLayout) view.findViewById(R.id.ll);
        appbar = (AppBarLayout) view.findViewById(R.id.appbar);
        collapsingtoolbarlayout = (CollapsingToolbarLayout) view.findViewById(R.id.collapsingtoolbarlayout);
        collapsingtoolbarlayout.setTitle(" ");
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        mainActivity.setSupportActionBar(toolbar);
        setting = (ImageView) view.findViewById(R.id.setting);
        setting.setOnClickListener(this);
        searching = (ImageView) view.findViewById(R.id.searching);
        searching.setOnClickListener(this);
        tvtitle = (TextView) view.findViewById(R.id.tv_title);
        autoPlayViewPager = (AutoPlayViewPager) view.findViewById(R.id.vp_main_banner);
        viewpager = (ViewPager) view.findViewById(R.id.viewpager);
        tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    }


    public void initData() {
        RefreshUtil.onRefresh(mainActivity, ll, refreshLayout);
        RefreshUtil.setOnRefreshListener(new RefreshUtil.OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                Toast.makeText(mainActivity, "开始下拉刷新了", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        RefreshUtil.onRefreshFinish();
                    }
                }, 3000);
            }
        });
        appbar.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if (state == State.EXPANDED) {
                    refreshLayout.setEnabled(true);
                    //展开状态
                } else if (state == State.COLLAPSED) {
                    refreshLayout.setEnabled(false);
                    //折叠状态
                } else {
                    refreshLayout.setEnabled(false);
                    //中间状态
                }
            }
        });
        if (viewpager != null) {
            setupViewPager(viewpager);
        }
        tabLayout.setupWithViewPager(viewpager);
        tabLayout.setSelectedTabIndicatorColor(mainActivity.getResources().getColor(R.color.refresh_bg));
        TabIconsUtil util = new TabIconsUtil(mainActivity, tabLayout, titles, tabIcons, tabIconsPressed);
        util.setTabIcons3();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int tag = (int) tab.getTag();
                if (0 == tag) {
                    AutoPlayUtil autoPlayUtil = new AutoPlayUtil(mainActivity, autoPlayViewPager, 4);
                    autoPlayUtil.getDataFromServer();
                } else if (1 == tag) {
                    AutoPlayUtil autoPlayUtil = new AutoPlayUtil(mainActivity, autoPlayViewPager, 7);
                    autoPlayUtil.getDataFromServer();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        AutoPlayUtil autoPlayUtil = new AutoPlayUtil(mainActivity, autoPlayViewPager, 4);
        autoPlayUtil.getDataFromServer();
    }

    private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(mainActivity.getSupportFragmentManager());
        adapter.addFragment(new DomesticFragment(mainActivity), "国内");
        adapter.addFragment(new InternationalFragment(), "国际");
        adapter.addFragment(new NoticeFragment(), "通知");
        viewPager.setAdapter(adapter);
    }

    static class Adapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting:
                mainActivity.startActivity(mainActivity, SettingActivity.class);
                break;
            case R.id.searching:
                mainActivity.startActivity(mainActivity, SearchingActivity.class);
                break;

        }
    }
}
